import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWT {
    @Test
    public void getJWT(){
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",1);
        claims.put("name","tom");
        String compact = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, "farmProduct") //签名和算法
                .setClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + 3600 * 1000)) //设置有效期
                .compact();
        System.out.println(compact);
    }
    @Test
    public void parse(){
        Claims body = Jwts.parser().setSigningKey("farmProduct")
                .parseClaimsJws("eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoidG9tIiwiaWQiOjEsImV4cCI6MTczNTIyNDIwOH0._QD2PzuGEe_c5cYTYFd3MEQtHmeGGWuBcvYuiVwK9IY")
                .getBody();
        System.out.println(body);
    }
}
